# -*- coding: utf-8 -*-
import maya.cmds as mc

import os, glob, math, re, sys, time

class setHeadsUpDisplay():
    def applyHUD(self):
        userName = self.getUserName()
        if not userName:
            return
        ##########################
        modelCameraName = ''
        modelCameraNameTemp = mc.getPanel(withFocus=True)        
        if (modelCameraNameTemp != 'outlinerPanel1') and (modelCameraNameTemp != 'graphEditor1'):
            modelCameraName = modelCameraNameTemp
        if not mc.getPanel(typeOf=modelCameraName)=='modelPanel':
            mc.warning('ѽ�����һ����ͼ�������Ȼ�Ҳ�֪�����ĸ�~')
            return
        mc.modelEditor(modelCameraName,e = True,locators=True)
        mc.modelEditor(modelCameraName,e = True,hud=False)
        if mc.objExists('Time'):
            TimeString = 'Time'+'|'+'testo'
            mc.setAttr(TimeString+'.text', self.getSysDate(),type = 'string') 
        if mc.objExists('File'):
            FileString = 'File'+'|'+'testo'
            mc.setAttr(FileString+'.text', self.getFileName(),type = 'string')     
        if mc.objExists('Animator'):
            FileString = 'Animator'+'|'+'testo'
            mc.setAttr(FileString+'.text', self.getUserName(),type = 'string')    
        if mc.objExists('Size'):
            FileString = 'Size'+'|'+'testo'
            mc.setAttr(FileString+'.text', self.getGlobalResolution(),type = 'string')    
        if mc.objExists('FocalLength'):
            FileString = 'FocalLength'+'|'+'testo'
            mc.setAttr(FileString+'.text','FL:'+ str(self.getFocalLength()),type = 'string')      
        #if mc.objExists('sequence'):
            #FileString = 'FocalLength'+'|'+'testo'
            #mc.setAttr(FileString+'.text', self.getFocalLength(),type = 'string') 
        if mc.objExists('CamName'):
            FileString = 'CamName'+'|'+'testo'
            mc.setAttr(FileString+'.text', self.getCameraName(),type = 'string')
        if mc.objExists('fps'):
            FileString = 'fps'+'|'+'testo'
            mc.setAttr(FileString+'.text', self.getFPS(),type = 'string')
    def getFPS(self):
        fpsString = mc.currentUnit(query=True,t=True)
        if fpsString == 'game':
            return '15fps'
        elif fpsString == 'film':
            return '24fps'
        elif fpsString == 'pal':
            return '25fps'
        elif fpsString == 'mtsc':
            return '30fps'
        elif fpsString == 'show':
            return '48fps'
        elif fpsString == 'palf':
            return '50fps'
        elif fpsString == 'ntscf':
            return '60fps'
        else:
            return 'error'
    def getSysDate(self):
        weekDayDict = {1:u'Mon', 2:u'Tue', 3:u'Wed', 4:u'Thu', 5:u'Fri', 6:u'Sat', 7:u'Sun'}
        localTime = time.localtime()
        dayForMat = '%Y/%m/%d'
        dayValue = time.strftime(dayForMat, localTime)
        weekForMat = '%w'
        weekValue = str(time.strftime(weekForMat, localTime))
        timeForMat = '%H:%M'
        timeValue = time.strftime(timeForMat, localTime)
        timeString = dayValue+" "+weekDayDict[int(weekValue)]+" "+timeValue
        return timeString
    '''
    def getUserName(self):
        scriptDir = mc.internalVar(usd=True)
        aboutDir = os.path.abspath(os.path.join(os.path.dirname(scriptDir), os.path.pardir))
        fileList = glob.glob(aboutDir+'/*.name')
        if fileList:
            fileName = os.path.basename(fileList[0])
            name = os.path.splitext(fileName)[0]
            return name
        else:
            name = self.setUserNameFile()
            return name
    '''
    def getUserName(self):
        # text, ok = QtGui.QInputDialog.getText(PySideUidata.getMayaWindow(), u'Your Name��', u'������������֣�')
        nameDir = mc.internalVar(userScriptDir=True)
        nameArry = nameDir.split("/")
        return nameArry[2]
        '''
        print nameArry[2]
        result = mc.promptDialog(t=u'Your Name��', m=u'������������֣������������ģ�����', b=['OK', 'Cancel'], db='OK', cb='Cancel', ds='Cancel')
        
        if result == 'OK':
            text = mc.promptDialog(query=True, text=True)
            if text:
                scriptDir = mc.internalVar(usd=True)
                aboutDir = os.path.abspath(os.path.join(os.path.dirname(scriptDir), os.path.pardir))
                fileName = open(unicode(aboutDir)+'/'+unicode(text)+unicode('.name'), 'w')
                fileName.write(u'����maya��Ƶ��Ϣ�����������ִ洢�ĵ���')
                fileName.close()
                return text
            else:
                mc.confirmDialog(t=u'���棡����', m=u'�����������Ч������', b=[u'�õ�'], db=u'�õ�')
                return ''
        else:
            return ''
            '''
    def getGlobalResolution(self):
        if mc.objExists('defaultResolution'):
            width = mc.getAttr('defaultResolution.w')
            height = mc.getAttr('defaultResolution.h')
            return str(width)+'*'+str(height)
        else:
            return '000'
    def getFocalLength(self):
        modelCameraName = ''
        modelCameraNameTemp = mc.getPanel(withFocus=True)
        if  (modelCameraNameTemp != 'outlinerPanel1') and (modelCameraNameTemp != 'graphEditor1'):
            modelCameraName = modelCameraNameTemp
        cameraName = ''
        if modelCameraName != '':
            cameraName = mc.modelEditor(modelCameraName, q=True, camera=True)
        if mc.objExists(cameraName):
            focalLength = int(mc.getAttr(cameraName+'.focalLength'))
        else:
            focalLength = ' '
        return focalLength
    def getStartFrame(self):
        time = str(int(mc.playbackOptions(q=True, minTime=True)))+'f'
        #print time
        return time
    def getEndFrame(self):
        time = str(int(mc.playbackOptions(q=True, maxTime=True)))+'f'
        return time
    def getCameraName(self):
        modelCameraName = ''
        modelCameraNameTemp = mc.getPanel(withFocus=True)
        if  (modelCameraNameTemp != 'outlinerPanel1') and (modelCameraNameTemp != 'graphEditor1'):
            modelCameraName = modelCameraNameTemp
        cameraName = ''
        if modelCameraName != '':
            cameraName = mc.modelEditor(modelCameraName, q=True, camera=True)
        if mc.objExists(cameraName):
            if mc.nodeType(cameraName)=='transform':
                return cameraName
            elif mc.nodeType(cameraName)=='stereoRigCamera':
                parentName = mc.listRelatives(cameraName, p=True)[0]
                return parentName
        else:
            return ''
    def getFileName(self):
        filePath = mc.file(q=True, sn=True)
        if filePath:
            fileName = os.path.basename(filePath)
            name = os.path.splitext(fileName)[0]
            return name
        else:
            return 'Untitled'
    def getFrameName(self):
        time = mc.currentTime(q=True)
        return time
    def getSequenceName(self):
        maxTime = mc.playbackOptions(q=True, maxTime=True)
        minTime = mc.playbackOptions(q=True, minTime=True)
        time = mc.currentTime(q=True)
        munsCon = '%0'+str(len("%d" % (maxTime-minTime)))+'d'
        return str(munsCon % (time-minTime+1))+'/'+str(int(maxTime-minTime+1))       
        
